(function () {
    'use strict';

    angular.module('BlurAdmin.pages.member')
        .controller('MemberEditCtrl', MemberEditCtrl);

    /** @ngInject */
    function MemberEditCtrl($scope,MemberModel,Constants,fileReader,$filter,AppUtils,$http,toastr,API,$state,$stateParams) {

        var vm = this;
        vm.files = null;

        vm.memberTypeArray = Constants.memberType;
        vm.params = {};//查询对象
        vm.page = {};
        vm.member = {};
        vm.member.memberType = "1";//默认老师
        vm.member.memberSex = "1";//默认性别男性
        //vm.member.avatarPath = "";

        var memberId = $stateParams.memberId;
        if(!AppUtils.isUndefinedOrNull(memberId)){
            MemberModel.getMemberInfo({id:memberId}).$promise.then(function(response){
                if(!AppUtils.isUndefinedOrNull(response.memberInfo)){
                    vm.member= _.clone(response.memberInfo);
                    if(!AppUtils.isUndefinedOrNull(vm.member.avatarPath)){
                        $scope.data.picture = vm.member.avatarPath;
                        $scope.data.noPicture = false;
                    }else{
                        $scope.data.picture = $filter('profilePicture')('default_head');
                        $scope.data.noPicture = true;
                    }
                }
            });
        }else{
            AppUtils.alert("未找到该成员！");
            AppUtils.goBack();
        }


        //打开时间选择弹出框==start
        $scope.data = {};
        $scope.data.showDatePicker= false;
        $scope.open2 = function() {
            $scope.data.showDatePicker = true;
        };
        //打开时间选择弹出框==end

        $scope.goBack = function () {
            AppUtils.goBack();
        }


        //图片上传预览====start
        $scope.removePicture = function () {
            var fileInput = document.getElementById('uploadFile');
            $scope.data.picture = $filter('profilePicture')('default_head');
            $scope.data.noPicture = true;
            fileInput.value = null;
        };
        $scope.uploadPicture = function () {
            var fileInput = document.getElementById('uploadFile');
            fileInput.click();
        };
        $scope.getFile = function () {
            var fileInput = document.getElementById('uploadFile').files[0];
            fileReader.readAsDataUrl(fileInput, $scope)
                .then(function (result) {
                    $scope.data.noPicture = false;
                    $scope.data.picture = result;
                });
            if(!AppUtils.isUndefinedOrNull(fileInput)){
                var formData=new FormData();
                formData.append("picUrl",fileInput);
                $http({
                    method: 'POST',
                    url:API + "/uploadImg",
                    headers: {'Content-Type':undefined},
                    transformRequest: angular.identity,
                    data:  formData
                }).success(function(data, status) {
                    vm.member.avatarPath = "";
                    if(data.stat == 'success'){
                        vm.member.avatarPath = data.path;
                    }
                    toastr.success('头像上传成功！');
                }).error(function(data, status) {
                    toastr.error('头像上传失败！');
                });
            }
        };
        //图片上传预览====end

        //保存成员信息====start
        $scope.submit = function() {
            MemberModel.pcSaveMemberInfo(vm.member).$promise.then(function(response){
                console.log(response);
                if(response.stat == 'success'){
                    toastr.success('成员信息保存成功！');
                    $state.go("member.list");
                }
            });
        };





    }

})();
